<template>
  <div id="dic-edit">
    <el-container>
      <el-main style="height: 510px">
        <el-form label-width="120px">
          <el-form-item style="width: 350px" label="上级字典">
            <el-input disabled v-model="choseItem.name" placeholder="请选择上级字典"></el-input>
          </el-form-item>
          <el-form-item label="上级字典选择器">
            <union-select ref="select" @choseItem="choseData" parent-id="parentId" :url="unionButton.target"
                          :permission="unionButton.id" init-value="0"></union-select>
          </el-form-item>
          <el-form-item style="width: 350px" label="字典名称">
            <el-input v-model="form.name" placeholder="请输入字典名"></el-input>
          </el-form-item>
          <el-form-item style="width: 350px" label="父级字典编号">
            <el-input disabled v-model="form.parentCode" placeholder="请输入字典编号"></el-input>
          </el-form-item>
          <el-form-item style="width: 350px" label="字典编号">
          <el-input v-model="form.code" placeholder="请输入字典编号"></el-input>
        </el-form-item>
          <el-form-item style="width: 350px" label="字典代码">
            <el-input v-model="form.coding" placeholder="请输入字典编号"></el-input>
          </el-form-item>
          <el-form-item style="width: 350px" label="字典状态码">
            <el-input v-model="form.status" placeholder="请输入字典状态码"></el-input>
          </el-form-item>
          <el-form-item style="width: 350px" label="字典描述">
            <el-input type="textarea" v-model="form.desc" placeholder="请输入字典描述"></el-input>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer height="100%">
        <el-divider></el-divider>
        <el-form label-width="120px">
          <el-form-item label="">
            <el-button @click="submit" type="primary" size="small" icon="el-icon-s-promotion">提交</el-button>
          </el-form-item>
        </el-form>
      </el-footer>
    </el-container>


  </div>
</template>

<script>
  import unionSelect from '../../../common/union-select'

  export default {
    components: {
      unionSelect
    },
    mounted() {
      this.initData()
    },
    data() {
      return {
        choseItem: {
          id: '0',
          parentId: '0',
          name: '',
          code:'',
        },
      }
    },
    methods: {
      submit() {
        if (!this.validate()) {
          return
        }
        this.common.request({
          url:this.button.target,
          param:this.form,
          permission:this.button.id,
          success:res=>{
            if (res.code == 1){
              this.$emit('save')
            }
          }
        })
      },
      validate() {
        this.form.parentId = this.choseItem.id
        if (this.common.isEmpty(this.form.parentId)) {
          this.form.parentId = '0'
        }
        if (this.common.isNotEmpty(this.form.parentCode) && this.common.isNotEmpty(this.form.code)){
          this.form.code = this.form.parentCode + "-" + this.form.code
        }
        let validateData = [{key: this.form.name, msg: '字典名'}, {
          key: this.form.parentId,
          msg: '父级字典'
        }, {key: this.form.code, msg: '字典编号'}]
        return this.common.validate(validateData)
      },
      initData() {
        if (this.common.isNotEmpty(this.form.parentId)) {
          this.choseItem.id = this.form.parentId
        }
        if (this.common.isNotEmpty(this.form.parentName)) {
          this.choseItem.name = this.form.parentName
        }
      },
      choseData(content) {
        this.choseItem = content
      }
    },
    props: {
      form: {
        type: Object,
        default() {
          return {
            id: '',
            name: '',
            code: '',
            coding:'',
            parentCode:'',
            parentId: '',
            status: '',
            desc: '',
          }
        }
      },
      unionButton: {
        type: Object
      },
      button:{
        type:Object
      }
    },
    name: "dic-edit",

  }
</script>

<style scoped>

</style>
